home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / comm / msged400.zip / src / strextra.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  3KB  |  179 lines

  1. /*
  2.  *  STREXTRA.C
  3.  *
  4.  *  Written on 30-Jul-90 by jim nutt.  Changes on 10-Jul-94 by John Dennis,
  5.  *  Paul Edwards and Andrew Clarke.  Released to the public domain.
  6.  *
  7.  *  A few string handling routines for Msged.
  8.  */
  9.  
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <stdlib.h>
  13. #include "strextra.h"
  14.  
  15. #if 0
  16. void strins(char *l, char c, int x)
  17. {
  18.     int i = strlen(l);
  19.  
  20.     if (x > (i + 1))
  21.         return;
  22.     else
  23.     {
  24.         x--;
  25.         memmove((l + x + 1), (l + x), (i - x) + 1);
  26.         *(l + x) = c;
  27.     }
  28. }
  29. #endif
  30.  
  31. void strdel(char *l, int x)
  32. {
  33.     int i = strlen(l);
  34.  
  35.     if (x > i)
  36.         return;
  37.     x--;
  38.     memmove((l + x), (l + x + 1), (i - x) + 1);
  39.     *(l + i) = 0;
  40. }
  41.  
  42.  
  43. /* strncmpi(...) -> strncmp(...), ignore case */
  44.  
  45. int strncmpi(const char *s, const char *t, size_t x)
  46. {
  47.     long n = (long)x;
  48.  
  49.     for (; n-- && (tolower(*s) == tolower(*t)); ++t)
  50.         if (!*s++)
  51.             return (0);         /* equal */
  52.  
  53.     if (n < 0)                  /* maximum hit */
  54.         return (0);             /* equal */
  55.  
  56.     return ((tolower(*s) > tolower(*t)) ? 1 : (-1));  /* not equal */
  57. }
  58.  
  59. #ifndef MSC
  60.  
  61. int stricmp(const char *s, const char *t)
  62. {
  63.     while (*s != '\0')
  64.     {
  65.         int rc;
  66.         rc = tolower((unsigned char)*s) - tolower((unsigned char)*t);
  67.         if (rc != 0)
  68.             return rc;
  69.         s++;
  70.         t++;
  71.     }
  72.     if (*t != '\0')
  73.         return (-tolower((unsigned char)*t));
  74.     return 0;
  75. }
  76. #endif
  77.  
  78. #ifndef __IBMC__
  79. char *strdup(const char *s)
  80. {
  81.     char *p;
  82.  
  83.     p = malloc(strlen(s) + 1);
  84.     if (p != NULL)
  85.     {
  86.         strcpy(p, s);
  87.     }
  88.     return (p);
  89. }
  90. #endif
  91.  
  92. #ifndef __IBMC__
  93. int memicmp(const void *s1, const void *s2, size_t n)
  94. #else
  95. int memicmp(void *s1, void *s2, size_t n)
  96. #endif
  97. {
  98.     size_t x;
  99.     int ret;
  100.  
  101.     for (x = 0; x < n; x++)
  102.     {
  103.         ret = (tolower((unsigned char)*(char *)s1)
  104.                - tolower((unsigned char)*(char *)s2));
  105.         if (ret != 0)
  106.         {
  107.             return (ret);
  108.         }
  109.     }
  110.     return (0);
  111. }
  112.  
  113. char *strlwr(char *s)
  114. {
  115.     char *p = s;
  116.  
  117.     while (*p != '\0')
  118.     {
  119.         *p = (char)tolower((unsigned char)*p);
  120.         p++;
  121.     }
  122.     return (s);
  123. }
  124.  
  125. char *strupr(char *s)
  126. {
  127.     char *p = s;
  128.  
  129.     while (*p != '\0')
  130.     {
  131.         *p = (char)toupper((unsigned char)*p);
  132.         p++;
  133.     }
  134.     return (s);
  135. }
  136.  
  137. #ifndef PACIFIC
  138.  
  139. const char *stristr(const char *s1, const char *s2)
  140. {
  141.     const char *pptr, *sptr, *start;
  142.     size_t slen, plen;
  143.  
  144.     for (start = s1, slen = strlen(s1), plen = strlen(s2);
  145.     /* while string length not shorter than pattern length */
  146.          slen >= plen;
  147.          start++, slen--)
  148.     {
  149.         /* find start of pattern in string */
  150.         while (toupper(*start) != toupper(*s2))
  151.         {
  152.             start++;
  153.             slen--;
  154.  
  155.             /* if pattern longer than string */
  156.  
  157.             if (slen < plen)
  158.                 return NULL;
  159.         }
  160.  
  161.         sptr = start;
  162.         pptr = s2;
  163.  
  164.         while (toupper(*sptr) == toupper(*pptr))
  165.         {
  166.             sptr++;
  167.             pptr++;
  168.  
  169.             /* if end of pattern then pattern was found */
  170.  
  171.             if (*pptr == '\0')
  172.                 return start;
  173.         }
  174.     }
  175.     return NULL;
  176. }
  177.  
  178. #endif
  179.